<?php

class Soul_Template_Html_Form_Field extends Soul_Template_Html_Container {

    private $_label = '';
    private $_description = '';
    private $_required = FALSE;
    private $_tag = 'tr';
    private $_itemTag = 'td';
    private $_form = NULL;
    private $_input = NULL;

    public function  __construct() {
        $this->addClass('field');
    }
    
    public function getItemTag(){
    	return $this->_itemTag;
    }
    
    public function setItemTag($value){
    	$this->_itemTag = $value;
    	return $this;
    }
    
    public function getTag(){
    	return $this->_tag;
    }
    
    public function setTag($value){
    	$this->_tag = $value;
    	return $this;
    }    
    
    public function getForm(){
        return $this->_form;
    }
    
    public function setForm($value){
        $this->_form = $value;
        return $this;
    }
    
    public function getInput(){
        return $this->_input;
    }
    
    public function setInput($value){
        $this->_input = $value;
        return $this;
    }

    public function getLabel() {
        return $this->_label;
    }
    public function setLabel($value) {
        $this->_label = $value;
        return $this;
    }
    
    public function setDescription($value){
    	$this->_description = $value;
    	return $this;
    }
    public function getDescription(){
    	return $this->_description;
    }

    public function getRequired() {
        return $this->_required;
    }
    public function setRequired($value) {
        $this->_required = $value;
        return $this;
    }
    
    public function renderBegin() {
    	return '<'.$this->getTag().' '.$this->getAttributeHtml().'>';
    }
    
    public function renderLabel(){
       	$result = '<'.$this->getItemTag().' class="field-label">';
       	$result .= $this->getLabel();
       	if($this->getRequired()) {
            $result .= '<span class="required">*</span>';
        }       
        return $result.'</'.$this->getItemTag().'>';
    }
    
    public function renderInput(){
       	$result = '<'.$this->getItemTag().' class="field-input">';
        $arr = Soul_Util_Array::pack($this->getInput());
        foreach($arr as $input){
            if($input instanceof Soul_Template_Html_Form_Input){
                if($this->getRequired()){
                    $input->addRequiredRule();
                }
                $this->getForm()->bind($input);
            }
            $result .= '&nbsp;'.$input;
        }   
        return $result.'</'.$this->getItemTag().'>';
    }    
    
    public function renderDescription(){
    	$result = '</'.$this->getItemTag().'>';
        $result .= '<'.$this->getItemTag().' class="field-desc"><span>'.$this->getDescription().'</span></'.$this->getItemTag().'>';
        return $result;
    }    
    
    public function renderEnd(){
        return '</'.$this->getTag().'>';
    }
    
    public function renderChildren() {
    	return $this->renderLabel().$this->renderInput().$this->renderDescription();
    }
}